﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;
using Entity;

namespace DAO
{
    public class QuanLyKhoanChiDAO
    {
        public static DataTable LayKhoanChi()
        {
            SqlConnection connect = null;
            DataTable rs = null;

            try
            {
                try
                {
                    connect = Connection.GetConnection();
                }
                catch
                {
                    throw new Exception("The connection with the database can´t be established");
                }
                String cmdText = "SELECT * FROM KHOAN_CHI";
                rs = SqlHelper.ExecuteDataset(connect, CommandType.Text, cmdText).Tables[0];
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                if (rs != null)
                    ((IDisposable)rs).Dispose();

                if (connect != null)
                    connect.Dispose();
            }
            return rs;
        }

        public static void SuaKhoanChi(KhoanChi kc)
        {
            SqlConnection connect = null;
            try
            {
                try
                {
                    connect = Connection.GetConnection();
                }
                catch
                {
                    throw new Exception("The connection with the database can´t be established");
                }

                SqlParameter[] param = new SqlParameter[4];
                param[0] = new SqlParameter("@makhoanchi", kc.makhoanchi);
                param[1] = new SqlParameter("@chitiet", kc.chitietkhoanchi);
                param[2] = new SqlParameter("@nguoinhan", kc.nguoinhan);
                param[3] = new SqlParameter("@sotien", kc.sotien);
                String cmdText = "UPDATE KHOAN_CHI SET ChiTietKhoanChi = @chitiet,TenNguoiNhan=@nguoinhan,SoTien=@sotien " +
                                "WHERE MaKhoanChi = @makhoanchi";
                SqlHelper.ExecuteNonQuery(connect, CommandType.Text, cmdText, param);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

            finally
            {
                if (connect != null)
                    connect.Dispose();
            }
        }

        public static void XoaKhoanChi(int makhoanchi)
        {
            SqlConnection connect = null;

            try
            {
                try
                {
                    connect = Connection.GetConnection();
                }
                catch
                {
                    throw new Exception("The connection with the database can´t be established");
                }

                SqlParameter[] par = new SqlParameter[1];
                par[0] = new SqlParameter("@makhoanchi", makhoanchi);
                String cmdText = "DELETE KHOAN_CHI WHERE MaKhoanChi = @makhoanchi";
                SqlHelper.ExecuteNonQuery(connect, CommandType.Text, cmdText, par);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                if (connect != null)
                    connect.Dispose();
            }
        }

        public static void ThemLoaiSP(KhoanChi kc)
        {
            SqlConnection connect = null;

            try
            {
                try
                {
                    connect = Connection.GetConnection();
                }
                catch
                {
                    throw new Exception("The connection with the database can´t be established");
                }

                SqlParameter[] par = new SqlParameter[4];
                par[0] = new SqlParameter("@chitiet", kc.chitietkhoanchi);
                par[1] = new SqlParameter("@nguoinhan", kc.nguoinhan);
                par[2] = new SqlParameter("@sotien", kc.sotien);
                par[3] = new SqlParameter("@ngaychi", DateTime.Now);
                String cmdText = "INSERT INTO KHOAN_CHI VALUES (@chitiet,@nguoinhan,@sotien,@ngaychi)";

                SqlHelper.ExecuteNonQuery(connect, CommandType.Text, cmdText, par);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                if (connect != null)
                    connect.Dispose();
            }
        }
    }
}
